package com.my.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.my.R;
import com.my.adapter.IconTextAdapter;
import com.my.service.CurriculumService;
import com.my.service.NightSpotService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HomeActivity extends AppCompatActivity {

    SharedPreferences preferences = null;
    ListView list;
    ListView mine;
    GridView function;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        preferences = HomeActivity.this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
        JSONObject user = JSONUtil.parseObj(preferences.getString("user", null));
        int userType = user.getInt("type");
        if(user == null) {
            startActivity(new Intent(this, LoginActivity.class));
        }
        BottomNavigationBar navigationBar = findViewById(R.id.btn_nav_bar);
        list = findViewById(R.id.list);
        mine = findViewById(R.id.mine);
        function = findViewById(R.id.function);
        List<IconTextAdapter.IconText> dataList = new ArrayList<IconTextAdapter.IconText>(){{
            add(new IconTextAdapter.IconText(R.drawable.ic_daqia, "打卡", "daka", userType == 3));
            add(new IconTextAdapter.IconText(R.drawable.ic_daka, "签到", "qiandao", userType == 3));
            add(new IconTextAdapter.IconText(R.drawable.ic_confirm, "晚点点名确认", "confirm", userType == 3 && user.getBool("rollCall")));
            add(new IconTextAdapter.IconText(R.drawable.ic_setting, "设置班长", "monitor", userType == 2));
            add(new IconTextAdapter.IconText(R.drawable.ic_rand, "随机选择学生", "rand", userType == 1));
        }};
        list.setAdapter(new IconTextAdapter(this, R.layout.adapter_icon_text, dataList.stream().filter(v -> v.vis).collect(Collectors.toList())));
        list.setOnItemClickListener((AdapterView<?> av, View v, int pos, long var4) -> {
            IconTextAdapter.IconText iconText = (IconTextAdapter.IconText) av.getItemAtPosition(pos);
            switch (iconText.tag) {
                case "daka":
                    startActivity(new Intent(this, MapActivity.class));
                    break;
                case "qiandao":
                    startActivity(new Intent(this, SignInActivity.class));
                    break;
                case "confirm":
                    startActivity(new Intent(this, NightSpotConfirmActivity.class));
                    break;
                case "monitor":
                    startActivity(new Intent(this, UserMonitorActivity.class));
                    break;
                case "rand":
                    startActivity(new Intent(this, RandUserActivity.class));
                    break;
                default:
                    break;
            }
        });
        mine.setAdapter(new IconTextAdapter(this, R.layout.adapter_icon_text, new ArrayList<IconTextAdapter.IconText>()){{
            add(new IconText(R.drawable.ic_update, "修改个人信息", "update"));
            add(new IconText(R.drawable.ic_exit, "退出当前账号", "exit"));
        }});
        mine.setOnItemClickListener((AdapterView<?> av, View v, int pos, long var4) -> {
            IconTextAdapter.IconText iconText = (IconTextAdapter.IconText) av.getItemAtPosition(pos);
            switch (iconText.tag) {
                case "update":
                    startActivity(new Intent(this, UserInfoActivity.class));
                    break;
                case "exit":
                    preferences.edit().remove("user").commit();
                    preferences.edit().remove("setting").commit();
                    preferences.edit().remove("absenteeismFirst").commit();
                    startActivity(new Intent(this, LoginActivity.class));
                    break;
                default:
                    break;
            }
        });
        List<IconTextAdapter.IconText> functionList = new ArrayList<IconTextAdapter.IconText>(){{
            add(new IconTextAdapter.IconText(R.drawable.ic_ask_leave, "请假申请", "ask_leave", userType == 3));
            add(new IconTextAdapter.IconText(R.drawable.ic_night_spot, "晚归申请", "night_spot", userType == 3));
            add(new IconTextAdapter.IconText(R.drawable.ic_ask_leave_review, "请假申请审核", "ask_leave_review"));
            add(new IconTextAdapter.IconText(R.drawable.ic_night_spot_review, "晚归申请审核", "night_spot_review", userType != 1));
            add(new IconTextAdapter.IconText(R.drawable.ic_attendance_log, "考勤打卡记录", "attendance_log"));
            add(new IconTextAdapter.IconText(R.drawable.ic_night_spot_log, "晚归打卡记录", "night_spot_log", userType != 1));
        }};
        function.setAdapter(new IconTextAdapter(this, R.layout.adapter_icon_text_grid, functionList.stream().filter(v -> v.vis).collect(Collectors.toList())));
        function.setOnItemClickListener((AdapterView<?> av, View v, int pos, long var4) -> {
            IconTextAdapter.IconText iconText = (IconTextAdapter.IconText) av.getItemAtPosition(pos);
            switch (iconText.tag) {
                case "ask_leave":
                    startActivity(new Intent(this, AskLeaveActivity.class));
                    break;
                case "night_spot":
                    startActivity(new Intent(this, NightSpotActivity.class));
                    break;
                case "ask_leave_review":
                    startActivity(new Intent(this, AskLeaveListActivity.class));
                    break;
                case "night_spot_review":
                    startActivity(new Intent(this, NightSpotListActivity.class));
                    break;
                case "attendance_log":
                    startActivity(new Intent(this, AttendanceLogActivity.class));
                    break;
                case "night_spot_log":
                    startActivity(new Intent(this, NightSpotLogActivity.class));
                    break;
                default:
                    break;
            }
        });
        navigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.ic_function, "功能"))
                .addItem(new BottomNavigationItem(R.drawable.ic_mine, "我的")).initialise();
        navigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        list.setVisibility(View.VISIBLE);
                        mine.setVisibility(View.GONE);
                        function.setVisibility(View.GONE);
                        break;
                    case 1:
                        list.setVisibility(View.GONE);
                        mine.setVisibility(View.GONE);
                        function.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        list.setVisibility(View.GONE);
                        mine.setVisibility(View.VISIBLE);
                        function.setVisibility(View.GONE);
                    default:
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) { }

            @Override
            public void onTabReselected(int position) { }
        });
        if(userType == 3) {
            Intent intent = new Intent(this, CurriculumService.class);
            startService(intent);
            intent = new Intent(this, NightSpotService.class);
            startService(intent);
        }
    }

    @Override
    protected void onRestart() {
        if(preferences.getString("user", null) == null) {
            startActivity(new Intent(this, LoginActivity.class));
        }
        super.onRestart();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 16908332) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}